<template>
  <el-container>
    <el-header>
      <a href="/"><img :src="picturePath" alt=""></a>
    </el-header>
    <div class="loginContainer">
      <el-card shadow="always">
        <div class="pic"></div>
        <div class="text-item">
          <el-form
            :model="formData"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item>
              <h2>欢迎登录</h2>
            </el-form-item>
            <el-form-item label="用户名" prop="username">
              <el-input
                type="text"
                v-model="formData.username"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                type="password"
                v-model="formData.password"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-link href="/Register" type="primary">无用户,注册</el-link>
              &nbsp;
              <el-link type="warning" @click="shopMallLogin">店铺登录</el-link>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >登录</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
    </el-container>

</template>

<script>
import { login } from "../api/user";
import { getWebInfo } from "../api/web";
import common from "../util/common"

export default {
  data() {
    return {
      localhostPath:common.localhostPath,
      picturePath:"",
      formData: {
        username: "",
        password: "",
      },
      rules: {
        // required 选择必填 trigger: 'blur' blur失去焦点
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.formData)
            .then(({ data }) => {
              if (data.code === 200) {
                console.log(data);
                localStorage.setItem("token", data.data.token);
                localStorage.setItem("refreshToken", data.data.refreshToken);
                localStorage.setItem("userId", data.data.id);
                localStorage.setItem("realname", data.data.nickName);
                this.$router.push("/");
                
              } else {
                this.$message({ type: "error", message: data.msg });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //店铺登录
    shopMallLogin() {
      this.$confirm("未拥有店铺账号, 是否注册?", "提示", {
        confirmButtonText: "注册",
        cancelButtonText: "登录",
        type: "warning",
      })
        .then(() => {
          // 点注册跳到店铺注册页
          this.$router.push("/StorRegister");
        })
        .catch(() => {
          // 点登录跳到后台登录
          window.location.href = "http://localhost:8081/Login";
        });
    },
  },
  mounted(){
    getWebInfo().then(({data})=>{
      this.picturePath = this.localhostPath + data.data.logoPath;
    })
  }
};
</script>

<style scoped>
.loginContainer {
  width: 100vw;
  height: calc(100vh - 80px);
  display: flex;
  justify-content: center;
  align-items: center;
}
.el-card {
  width: 900px;
  height: 450px;
}
.pic {
  background: url("../assets/onlinestor.png") no-repeat;
  width: 600px;
  height: 450px;
  position: absolute;
}
.text-item {
  position: relative;
  width: 300px;
  height: 450px;
  /* display: flex;
  align-items: center; */
  margin-left: 500px;
}
.el-link {
  display: inline;
}
.el-form-item__content {
  line-height: 0px;
}
</style>